package com.zhq.blog.core.domain.product;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.zhq.blog.core.domain.BasePersistentDomain;
import com.zhq.blog.core.domain.money.Card;
import com.zhq.blog.core.domain.search.PrizeSearch;

@Entity
public class Prize extends BasePersistentDomain<PrizeRepository> {
	
	private long categoryId;// 分类ID
	private long cardId;//对应的卡类型
	private String name;    //奖品名称
	private long price;		//奖品价格
	private String url;		//奖品连接
	private long send;  //自动发货，0自动，1不自动
	private long discount;//打折奖品 0打折 1不打折
	private long discount_type;//打折类型 0虚拟 1 实物
	private String picture;//图片路径
	private String exchange;//兑换
	private long exchange_num;//兑换次数
	private String recommend;//介绍
	
	@Transient
	private Category category;// 
	@Transient
	private Card card;// 
	
	public Prize(){}
	
	
	public Prize(long categoryId, long cardId, String name, long price,
			String url, long send, long discount, long discount_type,
			String picture, String exchange, long exchange_num, String recommend) {
		super();
		this.categoryId = categoryId;
		this.cardId = cardId;
		this.name = name;
		this.price = price;
		this.url = url;
		this.send = send;
		this.discount = discount;
		this.discount_type = discount_type;
		this.picture = picture;
		this.exchange = exchange;
		this.exchange_num = exchange_num;
		this.recommend = recommend;
	}
	public long getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(long categoryId) {
		this.categoryId = categoryId;
	}
	public long getCardId() {
		return cardId;
	}
	public void setCardId(long cardId) {
		this.cardId = cardId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getPrice() {
		return price;
	}
	public void setPrice(long price) {
		this.price = price;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public long getSend() {
		return send;
	}
	public void setSend(long send) {
		this.send = send;
	}
	public long getDiscount() {
		return discount;
	}
	public void setDiscount(long discount) {
		this.discount = discount;
	}
	public long getDiscount_type() {
		return discount_type;
	}
	public void setDiscount_type(long discount_type) {
		this.discount_type = discount_type;
	}
	public String getPicture() {
		return picture;
	}
	public void setPicture(String picture) {
		this.picture = picture;
	}
	public String getExchange() {
		return exchange;
	}
	public void setExchange(String exchange) {
		this.exchange = exchange;
	}
	public long getExchange_num() {
		return exchange_num;
	}
	public void setExchange_num(long exchange_num) {
		this.exchange_num = exchange_num;
	}
	public Category getCategory() {
		return category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}
	public String getRecommend() {
		return recommend;
	}
	public void setRecommend(String recommend) {
		this.recommend = recommend;
	}


	/**
	 * 初始化信息
	 */
	public void init() {
		category =context.getCategoryRepository().findOne(categoryId);
		card = context.getCardRepository().findOne(categoryId);
	}
	
	public  Specification<Prize> selectCondition(final PrizeSearch prizeSearch) {
		return new Specification<Prize>() {
			@Override
			public Predicate toPredicate(Root<Prize> root,
					CriteriaQuery<?> query, CriteriaBuilder builder) {

				Predicate predicate = builder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				expressions.add(builder.equal(root.<Boolean> get("deleted"),0));
				if (StringUtils.isNotBlank(prizeSearch.getName())) {
					expressions.add(builder.or( builder.like(root.<String> get("name"), "%"+ prizeSearch.getName() + "%")));

					
				}
				if (StringUtils.isNotBlank(prizeSearch.getCategoryId())) {
					expressions.add(builder.equal(root.<Long> get("categoryId"),prizeSearch.getCategoryId())); // 品牌id

					
				}

				return query.where(predicate).getRestriction();

			}
		};
	}
	
	
	
}
